using System.Security.Principal;
using Sedna.API.Security;
using Sedna.API.Session;
using Sedna.Core;
using Sedna.Core.Security;

namespace Sedna.API
{
    public class SecurityAPI: ISecurityAPI
    {
        private ISessionManager sessionManager;
        private IAuthenticator authenticator;


        public ISessionManager SessionManager
        {
            set { sessionManager = value; }
        }

        public IAuthenticator Authenticator
        {
            set { authenticator = value; }
        }

        public IToken Login(string login, string password)
        {
            IPrincipal principal = authenticator.Authenticate(login, password);
            
            if (principal == null)
            {
                throw new SednaException(SednaExceptionCodes.NotAuthenticated, "Wrong login or password!");
            }
            
            return sessionManager.CreateSession(principal).SessionKey;  
        }

        public void Logout(IToken sessionKey)
        {
            authenticator.SignOut(sessionManager.CurrentSession.Principal);
            sessionManager.DestroySession(sessionKey);    
        }


    }
}

